%% Using VideoWriter

%This is taken from the VideoWriter doc page
%See doc page for more details
writerObj = VideoWriter('peaks.avi');
open(writerObj);
Z = peaks; 
surf(Z); 
axis tight
set(gca,'nextplot','replacechildren');
set(gcf,'Renderer','zbuffer');
for k = 1:20 
   surf(sin(2*pi*k/20)*Z,Z)
   frame = getframe();
   writeVideo(writerObj,frame);
end

close(writerObj);

%% Making a movie of our ballistic trajectory

%Most of this is copied from Jan_29.m

g=9.8 % (m/s/s)
LaunchAngleDeg = 45 % angle in degrees
LaunchAngle = LaunchAngleDeg*pi/180
Speed0 = 10 % initial speed (m/s)

X0=0;
Y0=.01;

Vx0=Speed0*cos(LaunchAngle);
Vy0=Speed0*sin(LaunchAngle);

aX = 0;
aY = -g;

x_t = @(x0,v0,a,t)x0 + v0*t + 1/2*a*t.^2

%Setup and intialize variable for our 'while' loop.
CurrentY = Y0;
TimeStep = .01 % (s)
Cnt=1;
CurrentTime = 0;
XvsT = []; %Start with empty array
YvsT = []; %Start with empty array
while CurrentY >0 %Stop if we hit the ground
    Time = (Cnt-1)*TimeStep; %Time point we are evaluating in this iteration
    CurrentX=x_t(X0,Vx0,aX,Time); %Use our function to calc position
    CurrentY=x_t(Y0,Vy0,aY,Time); %Use our function to calc position
    XvsT(Cnt)=CurrentX; %Write position in next element of array
    YvsT(Cnt)=CurrentY;  %Write position in next element of array
    Cnt = Cnt +1; %Increment our counter.
end

T = size(XvsT,2); %Number of array elements 

% NEW: Add code to write frames to file
% NEW: Create VideoWriter object and open the file for writing
writerObj = VideoWriter('Ballistic.avi');
open(writerObj);

figure %Create figure
Ax = axes() %Create axes and store as variable for later use. 

for tt=1:T  %loop over each time point
    plot(XvsT(1:tt),YvsT(1:tt)) %plot trajectory up to element 'tt'
    %Note the indexing:  This means return the elements from 1 to tt
    hold on %next plot will write on top of existing (keeping existing)
    plot(XvsT(tt),YvsT(tt),'ro','linewidth',5) %Plot the objects current position
    Ax.XLim = [min(XvsT) max(XvsT)]; %Set the axis limits to the limits of total trajctory
    Ax.YLim = [min(YvsT) max(YvsT)];
    xlabel('X (m)');
    ylabel('Y (m)');
    pause(TimeStep);  %Pause for 'TimeStep' seconds. 
    
    %NEW: get the frame from figure and write to file
    frame = getframe();
    writeVideo(writerObj,frame);
   
   
    hold off %Next plot will make erase old plots
end

%NEW: close file
close(writerObj);

%%  Indexing using logicals

%Create an array
X=(-2:2)
Y=(-2:2)

%Create X and Y grids 
[XGrid,YGrid]=meshgrid(X,Y)

%Calculate an array of distances from center
R=sqrt(XGrid.^2+YGrid.^2)


R==0    %This returns a logical matrix where 

R(R==0) %This returns all elements where the logical elements are true

R(R==0)=100 %This sets all elements where logical array elements are true. 

R(R>0)'  %Another type of logical statements

%%  Numerical Integration Using the Midpoint Method

%Limits of integration
A=0
B=100

% This shows what our function looks like
FineSample=linspace(A,B,1000);
FineSine=sin(FineSample); %We are using the 'sin' function here
figure
plot(FineSample,FineSine)
xlabel('X')
ylabel('f(X)')

% Now we will integrate 'sin(x)' from A to B
NInterval = 100; %Number of intervals 

%We can create an array of midpoints this way:
EndPoints = linspace(A,B,NInterval+1)% +1 because we have end points
DeltaX = EndPoints(2)-EndPoints(1) %Spacing of intevals
MidPoints = EndPoints(1:end-1)+DeltaX/2 %Note the 'end-1' indexing

%Setup our loop for integrating
IntResult=0; %The accumulated intergral results
IntResultArray=zeros(NInterval,1); %The running integral results
for nn=1:NInterval %loop over midpoints
    Area  = sin(MidPoints(nn))*DeltaX; %This is the rectangular area
    IntResult = IntResult +Area; %Add that to our accumulated integral
    IntResultArray(nn)=IntResult; %Store running integral
end

%Compare our result to the exact analytical result.  Try changing NInterval
AnalyticResult = -cos(B) + cos(A)
IntResult
AnalyticResult-IntResult

%We can also look at how the numerical integral is performing as it
%accumulates
figure
plot(FineSample,-cos(FineSample) + cos(A))
hold on
plot(MidPoints,IntResultArray)
xlabel('X')
ylabel('Int f(X)')
legend('Analytic','Numeric')


